package com.puppycrawl.tools.checkstyle.checks.temp;

import com.puppycrawl.tools.checkstyle.StatelessCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;

/**
 * TODO 简要描述
 *
 * @author wkmcyz@163.com.
 * @since 09-23-2020
 */
@StatelessCheck
public class MethodStartWithAndroidCheck extends AbstractCheck {
    @Override
    public int[] getDefaultTokens() {
        return getAcceptableTokens();
    }

    @Override
    public int[] getAcceptableTokens() {
        return new int[]{
                TokenTypes.METHOD_DEF,
        };
    }

    private boolean enabled = true;

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    @Override
    public int[] getRequiredTokens() {
        return CommonUtil.EMPTY_INT_ARRAY;
    }

    @Override
    public void visitToken(DetailAST ast) {
        super.visitToken(ast);
        if (!enabled) {
            return;
        }
        DetailAST identToken = ast.findFirstToken(TokenTypes.IDENT);
        if (identToken == null) {
            throw new IllegalArgumentException("function has no name token");
        }
        if (!identToken.getText().startsWith("Android")) {
            log(ast, "Method {0} did not starts with `Android`.", identToken.getText());
        }
    }
}
